import React from 'react';
import { Tooltip, message } from 'antd';
import { history } from 'umi'
import { connect } from 'umi';
import AuthComponent from '@/utils/auth/AuthComponent';

import { useRequest } from '@/hooks/';
import BizIcon from '@/components/BizIcon';
import { queryProcess } from '@/pages/WorkFlow/Flow/services';
import { getProcessInfo } from '@/pages/Recruitment/service';

function ApprovalLink({ title = '查看录用审批流程', type = 'chakanfabuxinxi', params = {}, user }) {
    const { processInstanceId } = params;
    const { employeeCode } = user || {};

    const { run: fetchProcessInfo } = useRequest(() => getProcessInfo(processInstanceId), {
        manual: true,
        onSuccess: res => {
            if (res) {
                const { code, data: resData, msg } = res;
                if (code === 10000) {
                    history.push({
                        pathname: '/personal/portal/waitApproval/approvalDetail',
                        query: {
                            taskId: resData?.id,
                            processInstanceName: '录用申请'
                        }
                    });
                } else {
                    message.error(msg);
                }
            } else {
                message.error('操作失败');
            }
        }
    });

    const { run: fetchProcess } = useRequest(() => queryProcess({ processInstanceId }), {
        manual: true,
        onSuccess: res => {
            if (res) {
                const { code, data: resData, msg } = res;
                if (code === 10000) {
                    const judgeUser = resData?.find(item => item.type === '审批人' && item.status === 2);
                    // 判断当前用户是审批人，并且当前处于审批中状态
                    if (judgeUser?.userList?.includes(employeeCode)) {
                        fetchProcessInfo();
                        return;
                    }
                    const initialUser = resData?.find(item => item.type === '发起人');

                    // 判断当前用户是发起人
                    if (initialUser?.userList?.includes(employeeCode)) {
                        history.push({
                            pathname: '/personal/portal/myApplication/detail',
                            query: {
                                processInstanceId,
                                processInstanceName: '录用申请'
                            }
                        });
                        return;
                    }
                    history.push({
                        pathname: '/personal/portal/myAuditRead/myAuditReadDetail',
                        query: {
                            processInstanceId
                        }
                    });
                } else {
                    message.error(msg);
                }
            } else {
                message.error('操作失败');
            }
        }
    });

    return (
        <AuthComponent code="office:recruitment:course:hire:detail:workflow">
            <Tooltip title={title}>
                <BizIcon type={type} style={{ cursor: 'pointer' }} onClick={fetchProcess} />
            </Tooltip>
        </AuthComponent>
    );
}

export default connect(({ user }) => ({ user: user.currentUser }))(ApprovalLink);
